#ifndef MASKING_H
#define MASKING_H

#include <vector>

#include <QImage>
#include <QColor>

namespace qrcode {

class Masking
{
private:
    int version;  //版本号
    int maskNum; //掩模图形编号
    int nMods;  //每边模块数
    std::vector<std::pair<int, int>> lockModule;  //锁定的位置
    QImage initImage;
    QImage finalImage;

    static const int DIR_ROW;  //行方向
    static const int DIR_COL;  //列方向

    //生成掩模图形
    //std::vector<QImage> MaskPatterns();
    //QImage GenerateMaskPattern(int mods, int num);
    //bool IsDarkModule(int &x, int &y, int &num);

    //得到待评估结果
    std::vector<QImage> MaskedImg();
    QImage MaskedConvert(QImage image, int n);
    bool WhetherToggle(int &x, int &y, int &n);
    void ConvertColor(QImage &image, int &x, int &y);
    bool IsLocked(int &x, int &y);

    //std::vector<QImage> MaskedImg(std::vector<QImage> maskPatterns);
    //QImage XORImgPattern(QImage img, QImage pattern);
    //QColor XORColor(QColor c1, QColor c2);

    //评估得分
    std::vector<int> EvaluationMaskedImg(std::vector<QImage> maskedImg);
    int EvaluationPoints(QImage &image);       //评估总分
    int EvaluationCondition1(QImage &image);  //评估条件1
    int EvaluationCondition2(QImage &image);  //评估条件2
    int EvaluationCondition3(QImage &image);  //评估条件3
    int EvaluationCondition4(QImage &image);  //评估条件4
    bool IsModule11311(QImage &image, int x, int y, int direction);
    bool IsDark(QColor color);
    bool IsSameColor(QColor c1, QColor c2);
    bool IsSameColor(QColor c1, QColor c2, QColor c3, QColor c4);
    bool IsSameColor(QColor c1, QColor c2, QColor c3, QColor c4, QColor c5);

    //选择最佳掩模图形
    int SelectBestMaskPattern(std::vector<int> &scores);

    bool Mask0(int &x, int &y);
    bool Mask1(int &x, int &y);
    bool Mask2(int &x, int &y);
    bool Mask3(int &x, int &y);
    bool Mask4(int &x, int &y);
    bool Mask5(int &x, int &y);
    bool Mask6(int &x, int &y);
    bool Mask7(int &x, int &y);

public:
    Masking(int version);
    void MakeMasking();
    void RemoveMask(QImage &image);


    int getMaskNum() const;
    void setMaskNum(int value);
    void setInitImage(const QImage &value);
    void setLockModule(const std::vector<std::pair<int, int> > &value);
    QImage getFinalImage() const;
};

}

#endif // MASKING_H
